package com.by.yz.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.chatuidemo.Constant;
import com.by.yz.chatuidemo.ui.ChatActivity;
import com.by.yz.custom.CircleNetworkImageView;
import com.by.yz.custom.TechnologyTeamDividerItemDecoration;
import com.by.yz.entity.Account;
import com.by.yz.entity.AskBean;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.QustionRESFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mrs yang on 2017/1/5.
 */
public class ManagerPatients extends BaseActivity {
    private RecyclerView resultListview;
    List<AskBean> datas=new ArrayList<AskBean>();
    SimpleStringRecyclerViewAdapter simpleStringRecyclerViewAdapter;
    AskBean askBean;
    String answer;
    UserTest userTest;
    static String nick=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.managerpatients);
        simpleStringRecyclerViewAdapter=new SimpleStringRecyclerViewAdapter(this,datas);

        initView();
        initDatas();
    }

    private void initDatas(){
        AccountRESTFul accountRESTFul=new AccountRESTFul();
        accountRESTFul.get(new AccountRESTFul.IhttpCb() {
            @Override
            public void ok(JSONObject response) {
                answer=ToJSonObject.jsonToBean(response.toString(), Account.class).getLogin();
                QustionRESFul qustionRESFul = new QustionRESFul();
                qustionRESFul.getQuestion(new QustionRESFul.MyGetCallBack() {
                    @Override
                    public void error(String error) {
                        Log.e("hj", "更新失败" + error);
                    }
                    @Override
                    public void ok(JSONArray response) throws JSONException {
                        for (int i = 0; i < response.length(); i++) {
                            JSONObject jsonObject = response.getJSONObject(i);
                            askBean = ToJSonObject.jsonToBean(jsonObject.toString(), AskBean.class);
                            if(askBean.getAnswer().equals(answer)){
                                datas.add(askBean);
                            }
                        }
                        simpleStringRecyclerViewAdapter .notifyDataSetChanged();
                    }
                });
                UserRESTFul userRESTFul=new UserRESTFul();
                userRESTFul.getUser(answer, new UserRESTFul.UserPOSTCallBack() {
                    @Override
                    public void ok(JSONObject response) throws JSONException {
                        userTest=ToJSonObject.jsonToBean(response.toString(),UserTest.class);
                        nick = userTest.getnickName();
                    }
                    @Override
                    public void error(String response) {
                        Log.e("user-error","获取用户表失败"+response);
                    }
                });
            }

            @Override
            public void error(String error) {

            }
        });
    }

    private void initView() {
        setToolbarTitle("管理我的患者");
        resultListview=(RecyclerView) findViewById(R.id.manager_patients);
        resultListview.setLayoutManager(new LinearLayoutManager(resultListview.getContext()));
        resultListview.setAdapter(simpleStringRecyclerViewAdapter );
        resultListview.addItemDecoration(new TechnologyTeamDividerItemDecoration(this,TechnologyTeamDividerItemDecoration.VERTICAL_LIST));
    }

    public static class SimpleStringRecyclerViewAdapter
            extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {

        private final TypedValue mTypedValue = new TypedValue();
        private int mBackground;
        private List<AskBean> mValues;



        public static class ViewHolder extends RecyclerView.ViewHolder {
            public AskBean tt;

            public final View mView;
            public final CircleNetworkImageView mImageView;
            public final TextView askName;
            public final TextView askTime;
            public final TextView question;
            public final TextView nickname;

            public ViewHolder(View view) {
                super(view);
                mView = view;
                mImageView = (CircleNetworkImageView) view.findViewById(R.id.answer_image);
                askName = (TextView) view.findViewById(R.id.askName);
                askTime = (TextView) view.findViewById(R.id.ask_time);
                question = (TextView) view.findViewById(R.id.question);
                nickname = (TextView) view.findViewById(R.id.nickname);
            }

        }

        public AskBean getValueAt(int position) {
            return mValues.get(position);
        }

        public SimpleStringRecyclerViewAdapter(Context context, List<AskBean> items) {
            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
            mBackground = mTypedValue.resourceId;
            mValues = items;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.quick_answer_item, parent, false);
            // view.setBackgroundResource(mBackground);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.tt = mValues.get(position);
            holder.askName.setText(mValues.get(position).getCreater());
            holder.mImageView.setDefaultImageResId(R.mipmap.headimg);
            holder.mImageView.setErrorImageResId(R.mipmap.headimg_error);
            final String creater=mValues.get(position).getCreater();
            new Thread(){
                @Override
                public void run(){
                    UserRESTFul userRESTFul=new UserRESTFul();
                    userRESTFul.getUser(creater, new UserRESTFul.UserPOSTCallBack() {
                        @Override
                        public void ok(JSONObject response) throws JSONException {
                            UserTest userTest;
                            JSONObject jsonObject=response;
                            userTest=ToJSonObject.jsonToBean(jsonObject.toString(),UserTest.class);
                            holder.askName.setText(userTest.getnickName());
                            String image=userTest.getHeadimg();
                            try {
                                holder.mImageView.setImageUrl(image, App.imageLoader);
                                Log.e("image","image");
                            }catch(Exception e) {
                                Log.e("image","null");
                                holder.mImageView.setImageUrl("http://img2.hao661.com/uploads/allimg/c150515/1431E91Z5M10-2194c1.jpg",App.imageLoader);
                            }
                        }
                        @Override
                        public void error(String response) {
                            Log.e("PCF-user-error","获取用户表失败"+response);
                        }
                    });
                }
            }.start();
            String s= mValues.get(position).getTitle().replaceAll("T", " ");
            String s1 = s.substring(0,16);
            holder.askTime.setText(s1);
            holder.question.setText(mValues.get(position).getContent());
            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, ChatActivity.class);
                    intent.putExtra(Constant.EXTRA_USER_ID, holder.tt.getCreater());
                    intent.putExtra("userName", holder.askName.getText().toString());
                    context.startActivity(intent);
                }
            });

            Glide.with(holder.mImageView.getContext())
                    .load(mValues.get(position).getQuestion_image())
                    .fitCenter()
                    .into(holder.mImageView);
        }

        @Override
        public int getItemCount() {
            return mValues.size();
        }
    }
}
